﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace spel
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        tom tom;
        jerry jerry;
        List<ost> ostList = new List<ost>();
        Texture2D textureJerry;
        Texture2D textureTom;
        Texture2D textureOst;
        Texture2D textureGameover;
        Boolean gameIsOver = false;
        Texture2D textureBakgrund;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);


            textureJerry = Content.Load<Texture2D>("jerry");
            jerry = new jerry(this, textureJerry, spriteBatch);
            jerry.Position = new Vector2(300, 400);

            textureTom = Content.Load<Texture2D>("tom");
            tom = new tom(this, textureTom, spriteBatch);
            tom.Position = new Vector2(0, 0);

            textureOst = Content.Load<Texture2D>("ost");
           

            ostList.Clear();
            for (int i = 0; i < 2; i++)
            {
                ostList.Add(new ost(this, textureOst, spriteBatch, new Vector2(i * 50, 2 * 40)));
                textureOst = Content.Load<Texture2D>("ost");
            }
            for (int i = 0; i < 2; i++)
            {
                ostList.Add(new ost(this, textureOst, spriteBatch, new Vector2((i + 2) * 80, 4 * 60)));
                textureOst = Content.Load<Texture2D>("ost");
            }
            for (int i = 0; i < 2; i++)
            {
                ostList.Add(new ost(this, textureOst, spriteBatch, new Vector2((i + 5) * 100, 6 * 40)));
                textureOst = Content.Load<Texture2D>("ost");
            }
            for (int i = 0; i < 2; i++)
            {
                ostList.Add(new ost(this, textureOst, spriteBatch, new Vector2((i + 7) * 70, 8 * 20)));
                textureOst = Content.Load<Texture2D>("ost");
            }
            for (int i = 0; i < 2; i++)
            {
                ostList.Add(new ost(this, textureOst, spriteBatch, new Vector2((i + 9) * 50, 9 * 40)));
                textureOst = Content.Load<Texture2D>("ost");
            }

            textureGameover = Content.Load<Texture2D>("tom-jerry");

            



        }
        

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {

            KeyboardState keyboardState = Keyboard.GetState();

            if (!gameIsOver)
            {
                jerry.Update(gameTime);
                tom.Update(gameTime);
                foreach (var ost in ostList)
                {
                    if (jerry.GetRectangle().Intersects(ost.GetRectangle()))
                    {
                        ostList.Remove(ost);
                        break;
                    }
                }
                if (jerry.GetRectangle().Intersects(tom.GetRectangle()))
                {
                    gameIsOver = true;
                }
            }
            else
            {
                if (keyboardState.IsKeyDown(Keys.Enter))
                {
                    gameIsOver = false;
                    this.Initialize();
                }
                else if (keyboardState.IsKeyDown(Keys.Escape))
                {
                    this.Exit();
                }
            }


            base.Update(gameTime);
        }



        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            if (!gameIsOver)
            {
                
                
                jerry.Draw(gameTime);
                tom.Draw(gameTime);
                foreach (var ost in ostList)
                {
                    ost.Draw(gameTime);
                }
                
            }
            else
            {
                spriteBatch.Begin();
                spriteBatch.Draw(textureGameover, new Vector2(0, 0), Color.White);
                spriteBatch.End();
            }
            base.Draw(gameTime);
        }

    }
}
